package com.icss.web.back.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.icss.common.controller.BaseController;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.goods.service.IGoodsDbService;
import com.icss.goods.vo.ReClassify;
import com.icss.goods.vo.ReClassifyEnum;
import com.icss.goods.vo.ReEval;
import com.icss.goods.vo.ReGoods;

/**
 * 商品操作
 * 
 * @author 刘鑫
 */
@Controller
@RequestMapping("/back/goods/")
public class GoodsBackController extends BaseController
{
	@Resource
	private IGoodsDbService goodsDbService;
	
	/**
	 * 分类列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/classifyList")
	public String classifyList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReClassify> apiResponse = this.goodsDbService.findCondListClassifyService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		return "/back/goods/classifyList";
	}

	/**
	 * 分类添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/classifyInsert")
	public String classifyInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 页面跳转 */
		return "/back/goods/classifyInsert";
	}

	/**
	 * 分类添加提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "classifyInsertSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String classifyInsertSubmit(HttpServletRequest request, HttpServletResponse response,ReClassify classify) throws ServletException, IOException
	{
		String pubTimeStr = request.getParameter("pubTimeStr");
		classify.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
		
		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.goodsDbService.saveOneClassifyService(classify);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		return apiResponse.toJSON().toJSONString(); 
	}

	/**
	 * 分类更新
	 * @param request
	 * @param response
	 */
	@RequestMapping("/classifyUpdate")
	public String classifyUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ReClassify> apiResponse = this.goodsDbService.findOneClassifyService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/back/goods/classifyUpdate";
		}
		return "/back/goods/classifyInfo";
	}

	/**
	 * 分类更新提交
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "classifyUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String classifyUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ReClassify> apiResponse = this.goodsDbService.findOneClassifyService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		ReClassify classify = apiResponse.getDataOneJava() ;
		
		boolean flag = false ; 
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String name = request.getParameter("name");
			String url = request.getParameter("url");
			String content = request.getParameter("content");
			String status = request.getParameter("status");
			String pubTimeStr = request.getParameter("pubTimeStr");
			
			/* 赋值 */
			classify.setName(name);
			classify.setUrl(url);
			classify.setContent(content);
			classify.setStatus(Byte.valueOf(status));
			classify.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
			
			flag = true ; 
		}
		
		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if(flag)
		{
			apiResponseDb = this.goodsDbService.updateOneClassifyService(classify);
		}
		
		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString() ;
	}

	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsList")
	public String goodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		String classifyId = request.getParameter("classifyId");
		if(classifyId == null)
		{
			classifyId = "" ; 
		}
		request.setAttribute("classifyId", classifyId);
		condMap.put("classifyId", classifyId);
		
		String dataType = request.getParameter("dataType");
		if(dataType == null)
		{
			dataType = "" ; 
		}
		request.setAttribute("dataType", dataType);
		condMap.put("dataType", dataType);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReGoods> apiResponse = this.goodsDbService.findCondListGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ReClassifyEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ReClassify> classifyApiResponse = this.goodsDbService.findCondListClassifyService(null, condMap);
		request.setAttribute("classifyResponse", classifyApiResponse.toJSON());
		return "/back/goods/goodsList";
	}

	/**
	 * 商品添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsInsert")
	public String goodsInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ReClassifyEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ReClassify> classifyApiResponse = this.goodsDbService.findCondListClassifyService(null, condMap);
		request.setAttribute("classifyResponse", classifyApiResponse.toJSON());
		return "/back/goods/goodsInsert";
	}

	/**
	 * 商品添加提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "goodsInsertSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String goodsInsertSubmit(HttpServletRequest request, HttpServletResponse response,ReGoods goods) throws ServletException, IOException
	{
		String pubTimeStr = request.getParameter("pubTimeStr");
		goods.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
		
		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.goodsDbService.saveOneGoodsService(goods);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		JSONObject resultJSON = apiResponse.toJSON();
		return resultJSON.toJSONString() ; 
	}

	/**
	 * 商品更新
	 * @param request
	 * @param response
	 */
	@RequestMapping("/goodsUpdate")
	public String goodsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<ReGoods> apiResponse = this.goodsDbService.findOneGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 查询所有的分类 */
			/* 查询所有的分类;只查询商品的分类 */
			condMap.clear();
			condMap.put("status", ReClassifyEnum.STATUS_ENABLE.getStatus());
			ApiResponse<ReClassify> classifyApiResponse = this.goodsDbService.findCondListClassifyService(null, condMap);
			request.setAttribute("classifyResponse", classifyApiResponse.toJSON());
			return "/back/goods/goodsUpdate";
		}
		return "/back/goods/goodsInfo";
	}

	/**
	 * 商品更新提交
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "goodsUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String goodsUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ReGoods> apiResponse = this.goodsDbService.findOneGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		ReGoods goods = apiResponse.getDataOneJava() ;
		
		boolean flag = false ; 
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String classifyId = request.getParameter("classifyId");
			String name = request.getParameter("name");
			String brief = request.getParameter("brief");
			String price = request.getParameter("price");
			String marketPrice = request.getParameter("marketPrice");
			String stockNum = request.getParameter("stockNum");
			String dataType = request.getParameter("dataType");
			String content = request.getParameter("content");
			String status = request.getParameter("status");
			
			/* 赋值 */
			goods.setClassifyId(Integer.valueOf(classifyId));
			goods.setName(name);
			goods.setBrief(brief);
			goods.setPrice(Double.valueOf(price));
			goods.setMarketPrice(Double.valueOf(marketPrice));
			goods.setStockNum(Integer.valueOf(stockNum));
			goods.setDataType(Byte.valueOf(dataType));
			goods.setContent(content);
			goods.setStatus(Byte.valueOf(status));
			
			String pubTimeStr = request.getParameter("pubTimeStr");
			goods.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
			
			flag = true ; 
		}
		
		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if(flag)
		{
			apiResponseDb = this.goodsDbService.updateOneGoodsService(goods);
		}
		
		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString() ;
	}
	
	/**
	 * 分类列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/evalList")
	public String evalList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String goodsId = request.getParameter("goodsId");
		if(goodsId == null)
		{
			goodsId = "" ; 
		}
		condMap.put("goodsId", goodsId);
		request.setAttribute("goodsId", goodsId);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReEval> apiResponse = this.goodsDbService.findCondListEvalService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		return "/back/goods/evalList";
	}
}